package com.metagurukul.metagranth.service;

import com.metagurukul.metagranth.exception.MetaSqlException;
import com.metagurukul.metagranth.model.dao.UserDAO;
import com.metagurukul.metagranth.util.Validator;
import com.metagurukul.metagranth.vo.User;

/**
 * This class handles the DAO classes related to User Database
 * @author : TeamMetaGranth
 */

public class UserService {

	/**
	 * This class authenticates the user 
	 * @param User user
	 * @return User
	 * @throws Exception
	 */
	
	public static User authentication(User user) throws Exception {
		User returnUser = null;
		if (!Validator.isNull(user)) {
			if ((!user.getUsername().isEmpty())
					&& (!user.getPassword().isEmpty())) {
				// calling to UserDAO to interact with Database
				returnUser = UserDAO.authenticate(user);
			}
		}
		return returnUser;
	}

	/**
	 * it will return -1 if user already exists.or no of row inserted into
	 * database(1 as only one user is inserted).return 0 if username or password
	 * is null return -2 if user object given is null.
	 * @param : User user
	 * @return int status
	 * @throws Exception
	 */

	public static int saveUserInformation(User user) throws Exception {
		boolean statusIfUserExists = false;
		int status = -1;
		try {
			if (!Validator.isNull(user)) {
				if ((!user.getUsername().isEmpty())
						&& (!user.getPassword().isEmpty())) {
					//  checking whether user is exists or not
					statusIfUserExists = UserService.ifUserExists(user);
					if (!statusIfUserExists) {
						status = UserDAO.save(user);
					} else {
						status = -1;// username already existed.
					}
				} else {
					status = 0;
				}
			} else {
				status = -2;
			}
			return status;
		} catch (Exception exception) {
			throw exception;
		}
	}

	/**
	 * This function will check weather the username entered by the user during
	 * signUp already exists with the same name in database or not
	 * @param User user
	 * @return boolean status
	 * @throws Exception
	 */

	private static boolean ifUserExists(User user) throws Exception {

		boolean status;
		try {
			User isUsernameExists = UserDAO.userSet(user);
			if (isUsernameExists == null) { 
				status = false;
			} else {
				status = true; // Username is not available
			}
			return status;
		} catch (Exception exception) {
			throw exception;
		}
	}

	/**
	 * This method validates password and conform password then saves in database
	 * @param User user
	 * @param String newPassword
	 * @param String conNewPassword
	 * @return int status
	 * @throws MetaSqlException 
	 */
	
	public static int changePassword(User user, String newPassword,
			String conNewPassword) throws MetaSqlException {
		int status = -1;
		
		// if any password is null then status is set as 0
		if (conNewPassword==null || conNewPassword.isEmpty() || newPassword==null || newPassword.isEmpty()) {
			status = 0;
		} else if (!conNewPassword.trim().equals(newPassword.trim())) { // if both passwords are not same then status is set as 1
			status = 1;
		} else { // otherwise password is changed
			user.setPassword(newPassword);
			if (changeCredentials(user)){
				status = 2;
			} else {
				status = 3;
			}
		}
		return status;
	}

	/**
	 * This method calls  USerDAO to change user name password of an user
	 * @param User user
	 * @return boolean status
	 * @throws MetaSqlException 
	 */
	
	private static boolean changeCredentials(User user) throws MetaSqlException {
		boolean status = true ;
		
		//  Credentials are changed if user is not null
		if (user != null) {
			UserDAO.changeCredentials(user);
		} else { // otherwise status can't be changed
			 status = false;
		}
		return status;
	}
	
	/**
	 * This method gets profile of a user
	 * @param User user
	 * @return User
	 * @throws MetaSqlException
	 */
	
	public static User getProfile(User user) throws MetaSqlException {
		User tempUser = null;
		// if user is not null then profile is got
		if (!Validator.isNull(user))  {
			tempUser = UserDAO.userSet(user);
		}
		return tempUser;
	}
}